import {http} from "@/utils/http";

export type UserResult = {
  success: boolean;
  data: {
    /** 头像 */
    avatar: string;
    /** 用户名 */
    username: string;
    /** 昵称 */
    nickname: string;
    /** 当前登录用户的角色 */
    roles: Array<string>;
    /** 按钮级别权限 */
    permissions: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

/** 登录 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>("post", "/auth/login", {data});
};

/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/auth/refresh-token", {
    data
  });
};

export const userListQuery = (data?: object) => {
  return http.request<UserResult>("post", "/users/list", {data});
};

export const addUser = (data?: object) => {
  return http.request<UserResult>("post", "/users", {data});
};

export const updateUser = (data?: object) => {
  return http.request<UserResult>("put", "/users", {data});
};

export const deleteUser = (id: number) => {
  return http.request<UserResult>("delete", `/users/${id}`);
};

export const updateUserPassword = (id: number, password: string) => {
  return http.request<UserResult>("put", `/users/password`, {
    data: {id, newPassword: password}
  });
};

export const updateUserRole = (userId: number, roleIds: number[]) => {
  return http.request<UserResult>("put", `/users/role`, {
    data: {userId, roleIds}
  });
};

export const updateUserStatus = (id: number, status: number) => {
  return http.request<UserResult>("put", `/users/status`, {
    data: {id, status}
  });
};
